<?php
function httpRequest($url,$params=array(),$method='get'){
	if(trim($url)==''||!in_array($method,array('get','post'))||!is_array($params)){
		return false;
	}
	switch($method){
		case 'get':
			$str='?';
			foreach($params as $k=>$v){
				$str.=$k.'='.$v.'&';
			}
			$str=substr($str,0,-1);
			$url.=$str;
			echo $url;
			$result = file_get_contents($url);
			break;
		case 'post':
			$options = array(
			'http' => array(
			'method' => 'POST',
			'content' => http_build_query($params),
			),
			);
			$result = file_get_contents($url, false, stream_context_create($options));
			break;
	}
	return $result;
}
function getRand($proArr) {
	$result = '' ;
	//概率数组的总概率精度
	$proSum = array_sum($proArr);
	//概率数组循环
	foreach ($proArr as $key => $proCur) {
		$randNum = mt_rand(1, $proSum);
		if ($randNum <= $proCur) {
			$result = $key;
			break;
		} else {
			$proSum -= $proCur;
		}
	}
	unset ($proArr);
	return $result;
}
function getPrize($prize_arr)
{
	/*$prize_arr = array(
			'0' => array('id' =>1,'min' =>1,'max' =>29,'content' =>'一等奖','v' =>2,'num'=>10),
			'1' => array('id' =>2,'min' =>302,'max' =>328, 'content' =>'二等奖','v' =>5,'num'=>8),
			'2' => array('id' =>3,'min' =>242,'max' =>268, 'content' =>'三等奖','v' =>10,'num'=>5),
			'3' => array('id' =>4,'min' =>182,'max' =>208, 'content' =>'四等奖','v' =>50,'num'=>3),
			'4' => array('id' =>5,'min' =>122, 'max' =>148, 'content' =>'五等奖','v' =>100,'num'=>2),
			'5' => array('id' =>6,'min' =>62, 'max' =>88, 'content' =>'六等奖','v' =>490,'num'=>1),
			'6' => array('id' =>7,'min' =>array(32, 92, 152, 212, 272, 332),'max' =>array(58, 118, 178, 238, 298, 358), 'content' =>'谢谢参与' , 'v' =>600,'num'=>0)
	);*/
	
	foreach ($prize_arr as $key => $val) {
		$arr[$val['id' ]] = $val['v' ];
	}
	$rid = getRand($arr); //根据概率获取奖项 id
	$res = $prize_arr[$rid-1]; //中奖项
	/*$min = $res['min' ];
	$max = $res['max' ];
	if($res['id']==7){ //七等奖
		$i = mt_rand(0, 5);
		$result['angle' ] = mt_rand($min[$i], $max[$i]);
	} else{
		$result['angle' ] = mt_rand($min, $max); //随机生成一个角度
	}
	$result['content' ] = $res['content'];
	$result['id']=$res['id'];
	$result['num']=$res['num'];*/
	$res['sncode']=strtoupper(substr(md5(time()),mt_rand(1,18), 15));
	return $res;
}
function isWeixinBrowser() {
	$agent = $_SERVER ['HTTP_USER_AGENT'];
	if (! strpos ( $agent, "icroMessenger" )) {
		return false;
	}
	return true;
}
function create_tables($db, $prefix = ''){
	//读取SQL文件
	$sql = file_get_contents(MODULE_PATH . 'Data/by_code.sql');
	$sql = str_replace("\r", "\n", $sql);
	$sql = explode(";\n", $sql);

	//替换表前缀
	//$orginal = C('ORIGINAL_TABLE_PREFIX');
	//$sql = str_replace(" `{$orginal}", " `{$prefix}", $sql);

	//开始安装
	show_msg('开始安装数据库...');
	foreach ($sql as $value) {
		$value = trim($value);
		if(empty($value)) continue;
		if(substr($value, 0, 12) == 'CREATE TABLE') {
			$name = preg_replace("/^CREATE TABLE IF NOT EXISTS `(\w+)` .*/s", "\\1", $value);
			$msg  = "创建数据表".$name;
			if(false !== $db->execute($value)){
				show_msg($msg . '...成功');
			} else {
				show_msg($msg . '...失败！', 'error');
			}
		} else {
			$msg=substr($value, 0, 12);
			if(false !== $db->execute($value)){
				show_msg($msg . '...成功');
			} else {
				show_msg($msg . '...失败！', 'error');
			}
		}

	}
}
/**
 * 及时显示提示信息
 * @param  string $msg 提示信息
 */
function show_msg($msg, $class = ''){
	echo "<script type=\"text/javascript\">showmsg(\"{$msg}\", \"{$class}\")</script>";
	flush();
	ob_flush();
}
?>